package com.jsm.twentyfour.service;

import com.jsm.twentyfour.constant.RoomAccountStatus;
import com.jsm.twentyfour.domain.Account;
import com.jsm.twentyfour.domain.RoomAccount;
import com.jsm.twentyfour.domain.RoomAccountExample;
import com.jsm.twentyfour.dto.AccountDTO;
import com.jsm.twentyfour.dto.RoomAccountDTO;
import com.jsm.twentyfour.mapper.BaseMapper;
import com.jsm.twentyfour.mapper.RoomAccountMapper;
import com.jsm.twentyfour.request.ListRoomAccountRequest;
import com.jsm.twentyfour.transfer.RoomAccountTransfer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * create by jsm on 2019/1/23
 */
@Service
public class RoomAccountService extends BaseService <RoomAccount, RoomAccountDTO, RoomAccountExample> {
    @Resource
    private RoomAccountMapper roomAccountMapper;
    @Resource
    private AccountService accountService;

    @Override
    public BaseMapper <RoomAccount, RoomAccountExample> mapper() {
        return roomAccountMapper;
    }

    @Override
    public Class <RoomAccountDTO> getDTOClass() {
        return RoomAccountDTO.class;
    }

    @Override
    public RoomAccountDTO transfer(RoomAccount roomAccount) {
        return RoomAccountTransfer.transfer(roomAccount);
    }

    public RoomAccountDTO getByUserIdRoomIdStatus(Long userId, Long roomId, Integer status) {
        RoomAccountExample example = new RoomAccountExample();
        example.createCriteria().andUserIdEqualTo(userId).andRoomIdEqualTo(roomId).andStatusEqualTo(status);
        return getByExample(example);
    }

    public List <AccountDTO> playerList(ListRoomAccountRequest request) {

        List <AccountDTO> list = new ArrayList <>();
        RoomAccountExample example = new RoomAccountExample();
        example.createCriteria().andRoomIdEqualTo(request.getRoomId()).andStatusEqualTo(RoomAccountStatus.IN);
        List <RoomAccountDTO> roomAccountDTOS = listByExample(example, 0, 0);
        if (roomAccountDTOS != null && roomAccountDTOS.size() > 0) {
            for (RoomAccountDTO roomAccountDTO : roomAccountDTOS) {
                list.add(accountService.getById(roomAccountDTO.getUserId()));
            }
        }
        return list;
    }
}
